//
//  RegistrationAgreementViewController.m
//  ZJControlsClass
//
//  Created by wandoufo on 2024/12/13.
//

#import "RegistrationAgreementViewController.h"
#import "HomeNetWorking.h"


@interface RegistrationAgreementViewController ()

@property (nonatomic,strong) HomeProtocolNetWorking * manager;
@property (nonatomic,strong) UITextView * textView;
@property (nonatomic,strong) UIButton * readBtn;


@end

@implementation RegistrationAgreementViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"注册协议";
    
    [self setUpUI];
    [self loadNet];
    // 添加倒计时属性
    self.readBtn.enabled = NO;
    [self.readBtn setTitle:@"请阅读(5s)" forState:UIControlStateNormal];
    [self.readBtn setBackgroundColor:[COLOR_RGB(50, 113, 237) colorWithAlphaComponent:0.5]];
    
    // 开始倒计时
    __block int countdown = 5;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        countdown--;
        if (countdown > 0) {
            [self.readBtn setTitle:[NSString stringWithFormat:@"请阅读(%ds)", countdown] forState:UIControlStateNormal];
        } else {
            [timer invalidate];
            self.readBtn.enabled = YES;
            [self.readBtn setTitle:@"我已阅读并同意" forState:UIControlStateNormal];
            [self.readBtn setBackgroundColor:COLOR_RGB(50, 113, 237)];
        }
    }];
    [timer fire];
    
    
    
}
-(void)setUpUI{
    
    [self.view addSubview:self.textView];
    [self.view addSubview:self.readBtn];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(StatusBarHeight);
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(5));
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(5));
        make.bottom.equalTo(self.view).with.offset(-ALIGN_HEIGHT_PT(80));
    }];
    
    [self.readBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view).with.offset(-ALIGN_HEIGHT_PT(20));
        make.size.equalTo(@(CGSizeMake(150, ALIGN_HEIGHT_PT(45))));
    }];
    
    
}


-(void)loadNet{
    
    
    
    [self.manager dispatchTaskWithParam:@{@"type":@"4"} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            
            NSString * str = [responseObject[@"data"][@"content"] stringByRemovingPercentEncoding];
            self.textView.text = str;
            
            
            
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
            return;
        }
    }];
}


#pragma mark -- 点击事件
-(void)respondsTo_readBtn{
    [self.navigationController popViewControllerAnimated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeRegisXieyiType)]){
        [self.delegate changeRegisXieyiType];
    }
    
    
}


#pragma mark -- 懒加载

-(UITextView *)textView{
    if (!_textView){
        _textView = [[UITextView alloc] init];
        _textView.frame = self.view.bounds;
        _textView.editable = NO;
        _textView.font = [UIFont systemFontOfSize:16];
    }
    return _textView;
}

-(UIButton *)readBtn{
    if (!_readBtn){
        _readBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_readBtn setTitle:@"我已阅读并同意" forState:(UIControlStateNormal)];
        _readBtn.titleLabel.font = FONT14;
        _readBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        _readBtn.layer.cornerRadius = 5;
        _readBtn.layer.masksToBounds = YES;
        [_readBtn addTarget:self action:@selector(respondsTo_readBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _readBtn;
}


-(HomeProtocolNetWorking *)manager{
    if (!_manager){
        _manager = [HomeProtocolNetWorking new];
    }
    return _manager;
}



@end
